package com.bocloud.cmp.server.controller.resource;

import java.util.HashMap;
import java.util.Map;

//import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.resource.NetworkService;
import com.bocloud.cmp.model.NetBean;
import com.bocloud.cmp.model.SwitchModel;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

@RestController
@RequestMapping("/network")
public class NetworkController {

	@Autowired
	private NetworkService networkService;

	/**
	 * 列表
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param simple
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) Boolean simple,
			@Value(Common.REQ_USER) RequestUser user) {
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return networkService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple);
	}

	/**
	 * 创建
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		return networkService.create(params, user.getId());
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/{id}/remove", method = { RequestMethod.DELETE })
	public BsmResult remove(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return networkService.remove(id, user.getId());
	}

	/**
	 * 详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
	public BsmResult detail(@PathVariable(value = Common.ID) Long id) {
		return networkService.detail(id);
	}

	@RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			NetBean bean = JSONObject.parseObject(object.toJSONString(), NetBean.class);
			return networkService.modify(bean, user.getId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 创建分布式交换机
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/createDSwitch", method = { RequestMethod.POST })
	public BsmResult createDSwitch(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			SwitchModel model = JSONObject.parseObject(object.toJSONString(), SwitchModel.class);
			return networkService.createDSwitch(model, user.getId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 虚拟机列表
	 * 
	 * @param id
	 *            任务ID
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{id}/servers", method = { RequestMethod.GET })
	public BsmResult servers(@PathVariable(value = Common.ID) Long id) {
		return networkService.servers(id);
	}

}
